package com.clinic.dao.imp;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.clinic.dao.interfaces.Daily_reservations_dao;
import com.clinic.domain.Daily_reservations;
import com.clinic.util.Util_imp;

public class Daily_reservations_imp implements Daily_reservations_dao {

	SessionFactory session_factory = null;

	public Daily_reservations_imp() {
		session_factory = Util_imp.get_sessionfactory();
	}

	@Override
	public int create_daily_reservation(Daily_reservations reservation) {
		System.out.println("Enter - create_daily_reservation");
		Session session = session_factory.openSession();
		Transaction tx = null;
		int dn_obj_id = 0;
		try {
			tx = session.beginTransaction();
			dn_obj_id = (Integer) session.save(reservation);
			tx.commit();
			System.out.println("New Daily_reservations Created with ID: " + dn_obj_id);
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Create Daily_reservations Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - create_daily_reservation");
		return dn_obj_id;
	}

	@Override
	public void update_daily_reservation(Daily_reservations ds) {
		System.out.println("Enter - update_daily_reservation");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(ds);
			tx.commit();
			System.out.println("Daily_reservations Object Updated");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Update Daily_reservations Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - update_daily_reservation");
	}

	@Override
	public void delete_daily_reservation(int ds_obj_id) {
		System.out.println("Enter - delete_daily_reservation");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Daily_reservations se = (Daily_reservations) session.get(Daily_reservations.class, ds_obj_id);
			session.delete(se);
			tx.commit();
			System.out.println("Daily_reservations with ID: " + ds_obj_id + " Deleted");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Update Daily_reservations Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - delete_daily_reservation");
	}

	@Override
	public Daily_reservations get_daily_reservations(int ds_obj_id) {
		System.out.println("Enter - get_clinic_config");
		Session session = session_factory.openSession();
		Transaction tx = null;
		Daily_reservations ds = null;
		try {
			tx = session.beginTransaction();
			ds = (Daily_reservations) session.createQuery("from Daily_reservations where ds_obj_id=:id")
					.setParameter("id", ds_obj_id).uniqueResult();
			tx.commit();
			System.out.println("Returned Reservation Type: " + ds.getReservation_type());
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_clinic_config");
		return ds;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Daily_reservations> get_all_daily_reservation() {
		System.out.println("Enter - get_all_daily_reservation");
		Session session = session_factory.openSession();
		List<Daily_reservations> l = null;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			l = session.createQuery("from Daily_reservations").list();
			tx.commit();
			System.out.println("Number of Daily_reservations returned is: " + l.size());
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_all_daily_reservation");
		return l;
	}

	@Override
	public Daily_reservations persistOrMerge(Daily_reservations d) {
		return (Daily_reservations) session_factory.getCurrentSession().merge(d);
	}

	@Override
	public int getsize() {
		System.out.println("Enter - getsize");
		Session session = session_factory.openSession();
		int size = 0;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			size = ((Long) session.createQuery("select count(*) from Daily_reservations").uniqueResult()).intValue();
			tx.commit();
			System.out.println("Count(*) Daily_reservations: " + size);
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - getsize");
		return size;
	}

}